package com.zhongxi.guoyuan.adapter.rewardCoin;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.Constants;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;
import com.zhongxi.guoyuan.utils.UiUtils;

import java.math.BigDecimal;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.zhongxi.guoyuan.R.string.get;

/**
 * Created by Administrator on 2017/10/13.
 */

public class ShopNewCateAdapter extends BaseAdapter {

    private Context context;
    private List<HotGoodsData> data;

    public ShopNewCateAdapter(Context context, List<HotGoodsData> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        if (data != null && data.size() > 0) {
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if (view == null) {
            view = View.inflate(context, R.layout.item_newgoods, null);
            vh = new ViewHolder(view);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        if (null != data.get(i) && !"".equals(data.get(i))) {
            HotGoodsData list = data.get(i);
            if (null != list.getThumbnail() && !"".equals(list.getThumbnail())) {
//                int width = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
//                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width * 1 / 3, (int) (width * 1 / 3 * Constants.detailRate));
//                vh.ivIcon.setLayoutParams(lp);
                Glide.with(context).load(Url.IMG_URL + list.getThumbnail()).placeholder(R.mipmap.iv_loading).into(vh.ivIcon);
            }
            if (null != list.getName() && !"".equals(list.getName())) {
                vh.tvProduct.setText(list.getName().trim());
            } else {
                vh.tvProduct.setText("");
            }
            if (null != list.getOrdernum() && !"".equals(list.getOrdernum())) {
                vh.tvSalesVolume.setText("销量：" + list.getOrdernum());
            } else {
                vh.tvSalesVolume.setText("");
            }

            if (TextUtils.isEmpty(list.getFloorprice())) {
                vh.tvPrice.setText("0 元");
            } else {
                vh.tvPrice.setText(list.getFloorprice() + " 元");
            }
            switch (list.getGoodstype()) {
                case "1":
                    if (TextUtils.isEmpty(list.getFloorprice())) {
                        vh.tvMd.setText("");
                        vh.tvHd.setText("");
                    } else {
                        vh.tvMd.setText(context.getResources().getString(R.string.give_meidou) + CommonTool.getWhole(list.getFloorprice()) + " 美豆");
                        vh.tvMd.setTextColor(context.getResources().getColor(R.color.postCartItem));
                        vh.tvHd.setText(context.getResources().getString(R.string.more_give) + DataUtil.multiplys(list.getFloorprice(), "0.05", 2) + "红豆");
                        vh.tvHd.setTextColor(context.getResources().getColor(R.color.title_red));
                    }
                    break;
//                case "2":
//                    holder.tv_reward_yield.setVisibility(View.GONE);
//                    if (list.getFloorprice().equals(list.getPeakprice())) {
//                        vh.tvMd.setText("立即赠 " + DataUtil.multiplys(list.getFloorprice(), "2") + " 美豆");
//                    } else {
//                        vh.tvMd.setText("立即赠" + DataUtil.multiplys(list.getFloorprice(), "2") + "美豆 ~ " + DataUtil.multiplys(list.getPeakprice(), "2") + "美豆");
//                    }
//                    break;
                case "3":
                    if (TextUtils.isEmpty(list.getFloorprice())) {
                        vh.tvMd.setText("");
                        vh.tvHd.setText("");

                    } else {
                        if (DataUtil.moreThanZero(CommonTool.getWhole(DataUtil.multiplys(list.getFloorprice(), Constants.REDBEAN_RATE, 4)))){
                            vh.tvHd.setText(context.getResources().getString(R.string.award_hongdou) + CommonTool.getWhole(DataUtil.multiplys(list.getFloorprice(), Constants.REDBEAN_RATE, 4)) + " 红豆");
                        }else
                        vh.tvHd.setText( "");
                        vh.tvHd.setTextColor(context.getResources().getColor(R.color.title_red));
                        vh.tvMd.setText(UiUtils.getString(R.string.goldenDesc)+ list.getFloorprice() + " 元");
                        vh.tvMd.setTextColor(context.getResources().getColor(R.color.title_red));
                    }
                    break;
            }
        }
        return view;
    }

    static class ViewHolder {
        @Bind(R.id.iv_icon)
        ImageView ivIcon;
        @Bind(R.id.tv_product)
        TextView tvProduct;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_md)
        TextView tvMd;
        @Bind(R.id.tv_hd)
        TextView tvHd;
        @Bind(R.id.tv_salesVolume)
        TextView tvSalesVolume;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
